#pragma once

#include <iostream>
#include <pthread.h>


class Mutex
{
public:
    Mutex(pthread_mutex_t * lock)
        :_lock(lock)
    {}

    ~Mutex()
    {}

    void lock()
    {
        if(_lock)
            pthread_mutex_lock(_lock);
    }

    void unlock()
    {
        if(_lock)
            pthread_mutex_unlock(_lock);
    }

private:
    pthread_mutex_t *_lock;
};


class lockGuard
{
public:
    lockGuard(pthread_mutex_t *lock)
        :_mutex(lock)
    {
        _mutex.lock();      // 在构造函数自动加锁
    }

    ~lockGuard()
    {
        _mutex.unlock();    // 在析构函数自动解锁
    }

private:
    Mutex _mutex;
};
